{
  "name": "Production Yield Deviation Reporting Agent",
  "nodes": [
    {
      "parameters": { "updates": ["message"] },
      "id": "tg-trigger-c3",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [0, 0]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "name": "user_message", "value": "={{ $json.message.text }}", "type": "string" },
            { "name": "user_name", "value": "={{ $json.message.from.first_name }}", "type": "string" },
            { "name": "chat_id", "value": "={{ $json.message.chat.id }}", "type": "string" }
          ]
        }
      },
      "id": "set-c3",
      "name": "Workflow Configuration",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [220, 0]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=User: {{ $json.user_name }}\nMessage: {{ $json.user_message }}",
        "options": {
          "systemMessage": "=You are a Production Yield Deviation Reporting Assistant.\n\nResponsibilities:\n1. Identify whether the user wants to REGISTER a new yield deviation or CHECK status.\n\n2. For new deviations, collect:\n   - Plant ID\n   - Production Batch ID\n   - Standard Yield (%)\n   - Actual Yield (%)\n   - Deviation Reason\n\n3. Validate Plant ID using Plant Master Tool and extract Plant_Name and Plant_Manager_Email.\n\n4. If valid:\n   - Generate Deviation ID: YLD-YYYYMMDD-[last4Plant]-HHmmss\n   - Register deviation in Yield Deviation Register Tool\n   - Send email to Plant_Manager_Email\n   - Explain investigation workflow\n   - Respond via Telegram\n\n5. For invalid plant:\n   - Explain reason\n   - Do not register\n   - Respond via Telegram\n\n6. For status check:\n   - Ask for Deviation ID\n   - Retrieve status using Yield Status Tool\n\nRules:\n- Do not assign blame\n- Do not promise resolution\n- Always use Telegram Response Tool"
        }
      },
      "id": "agent-c3",
      "name": "Yield Deviation Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3,
      "position": [500, 0]
    },
    {
      "parameters": { "model": { "mode": "id", "value": "gpt-4o" } },
      "id": "llm-c3",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [320, 220]
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Workflow Configuration').item.json.chat_id }}"
      },
      "id": "memory-c3",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [500, 220]
    },
    { "id": "plant-master-c3", "name": "Plant Master Tool", "type": "n8n-nodes-base.googleSheetsTool", "typeVersion": 4.7, "position": [640, 260] },
    { "id": "yield-register-c3", "name": "Yield Deviation Register Tool", "type": "n8n-nodes-base.googleSheetsTool", "typeVersion": 4.7, "position": [800, 260] },
    { "id": "yield-status-c3", "name": "Yield Status Tool", "type": "n8n-nodes-base.googleSheetsTool", "typeVersion": 4.7, "position": [960, 320] },
    {
      "parameters": {
        "sendTo": "={{ $fromAI('plant_manager_email') }}",
        "subject": "={{ $fromAI('email_subject') }}",
        "message": "={{ $fromAI('email_body') }}"
      },
      "id": "gmail-c3",
      "name": "Yield Deviation Email",
      "type": "n8n-nodes-base.gmailTool",
      "typeVersion": 2.1,
      "position": [1120, 220]
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "={{ $json.output }}"
      },
      "id": "tg-response-c3",
      "name": "Send Telegram Response",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [960, 0]
    }
  ],
  "connections": {
    "Telegram Trigger": { "main": [[{ "node": "Workflow Configuration", "type": "main" }]] },
    "Workflow Configuration": { "main": [[{ "node": "Yield Deviation Agent", "type": "main" }]] },
    "Yield Deviation Agent": { "main": [[{ "node": "Send Telegram Response", "type": "main" }]] }
  }
}
